# -*- coding: utf-8 -*-
# @Date：2024/9/30 11:54:25
# @Author：CJiaの青姝
# @FileName：userinfo
# @Editor：PyCharm2024
# @Remark：
from django.core.cache import cache
from auths.models import User
from utils.cj_jwt import decode_token


def get_user_info_from_token(request):
    token = request.META.get("HTTP_AUTHORIZATION")
    if token is None:
        return None
    token = token.replace("Bearer ", "")
    # token解析结果
    result = decode_token(token)
    # 获取用户
    user = User.objects.get(id=result['data']['id'])
    # 返回结果
    return user


def get_user_logout_token(request):
    # 获取Token
    authorization = request.META.get("HTTP_AUTHORIZATION")
    token = authorization.replace('Bearer ', "")  # 分割token
    # token解析结果
    result = decode_token(token)
    # 注销用户不用判断是否过期
    # 刪除缓存
    cache.delete_pattern(result['data']["email"] + "_Token")
    # 删除加密盐
    cache.delete_pattern(result['data']["email"] + "_salt")
    # 返回结果
    return "注销成功"
